#pragma once
#include <iostream>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <memory>
#include "Mutex.hpp"

namespace LogModule
{
    using namespace LockModule;
        // 获取一下当前系统的时间
    std::string CurrentTime()
    {
        time_t time_stamp = ::time(nullptr);
        struct tm curr;
        localtime_r(&time_stamp, &curr); // 时间戳，获取可读性较强的时间信息5

        char buffer[1024];
        // bug
        snprintf(buffer, sizeof(buffer), "%4d-%02d-%02d %02d:%02d:%02d",
                 curr.tm_year + 1900,
                 curr.tm_mon + 1,
                 curr.tm_mday,
                 curr.tm_hour,
                 curr.tm_min,
                 curr.tm_sec);

        return buffer;
    }

    const std::string defaultlogpath = "./log/";
    const std::string defaultlogname = "log.txt";

    //日志等级
    enum class LogLevel
    {
        DEBUG = 1,
        INFO,
        WARNING,
        ERROR,
        FATAL
    };

    std::string Level2String(LogLevel level)
    {
        switch(level)
        {
            case LogLevel::DEBUG:
                return "DEBUG";
            case LogLevel::INFO:
                return "INFO";
            case LogLevel::WARNING:
                return "WARNING";
            case LogLevel::ERROR:
                return "ERROR";
            case LogLevel::FATAL:
                return "FATAL";
            default:
                return "None";
        }
    }

    //刷新策略
    class LogStrategy
    {
    public:
        virtual ~LogStrategy() = default;
        virtual void SyncLog(const std::string& message) = 0;   
    };
    class ConsoleLogStrategy:public LogStrategy
    {
    public:
        ConsoleLogStrategy()
        {

        }
        ~ConsoleLogStrategy()
        {

        }
        void SyncLog(const std::string& message)
        {
            LockGuard lockguard(_lock);
            std::cout<<message<<std::endl;
        }
    private:
        Mutex _lock;
    };
    
    class FileLogStrategy: public LogStrategy
    {
    public:
        FileLogStrategy(const std::string& logpath = defaultlogpath,const std::string& logname = defaultlogname)
        :_logpath(logpath)
        ,_logname(logname)
        {
            LockGuard lockguard(_lock);

            if(std::filesystem::exists(_logpath))
            {
                return;
            }
            try
            {
                std::filesystem::create_directories(_logpath);
            }
            catch(std::filesystem::filesystem_error& e)
            {
                std::cerr << e.what() << '\n';
            }
            
        }
        ~FileLogStrategy()
        {

        }
        void SyncLog(const std::string& message)
        {
            LockGuard lockguard(_lock);
            std::string log = _logpath + _logname;
            std::ofstream out(log,std::ios::app);
            if(!out.is_open())
            {
                return;
            }
            out<<message<<"\n";
            out.close();
        }
    private:
        std::string _logpath;
        std::string _logname;
        Mutex _lock;
    };


    class Logger
    {
    public:
        Logger()
        {
            _strategy = std::make_shared<ConsoleLogStrategy>();
        }
        void EnableConsoleLog()
        {
            _strategy = std::make_shared<ConsoleLogStrategy>();
        }
        void EnableFileLog()
        {
            _strategy = std::make_shared<FileLogStrategy>();
        }

        ~Logger()
        {

        }
        class LogMessage
        {
        public:
            LogMessage(LogLevel level,const std::string& filename,int line,Logger& logger)
            :_curtime(CurrentTime()),
             _level(level),
             _pid(getpid()),
             _filename(filename),
             _line(line),
             _logger(logger)
            {
                std::stringstream ssbuffer;
                ssbuffer << "[" << _curtime << "] "
                         << "[" << Level2String(_level) << "] "
                         << "[" << _pid << "] "
                         << "[" << _filename << "] "
                         << "[" << _line << "] - ";
                _loginfo = ssbuffer.str();
            }
            template<typename T>
            LogMessage& operator<<(const T& info)
            {
                std::stringstream ss;
                ss<<info;
                _loginfo+=ss.str();
                return *this;
            }
            ~LogMessage()
            {
                if(_logger._strategy)
                {
                    _logger._strategy->SyncLog(_loginfo);
                }
            }
        private:
            std::string _curtime;
            std::string _filename;
            LogLevel _level;
            pid_t _pid;
            int _line;
            Logger& _logger;
            std::string _loginfo;
        };
        LogMessage operator()(LogLevel level,const std::string & filename,int line)
        {
            return LogMessage(level,filename,line,*this);
        }
    private:
        std::shared_ptr<LogStrategy> _strategy;//刷新策略
    };
    Logger logger;
#define LOG(level) logger(level,__FILE__,__LINE__)
#define ENABLE_CONSOLE_LOG() logger.EnableConsoleLog()
#define ENABLE_FILE_LOG() logger.EnableFileLog()
}